package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.constant.IsStatus;
import com.prd.mapper.EntNoticeMapper;
import com.prd.model.EntNotice;
import com.prd.model.EntNoticeRecipient;
import com.prd.model.LoginUser;
import com.prd.service.EntNoticeRecipientService;
import com.prd.service.EntNoticeService;
import com.prd.service.UserService;
import com.prd.util.StringsUtils;
import com.prd.util.UserInfoUtil;
import com.prd.vo.EntNoticeVo;
import com.prd.vo.RecipientVo;
import com.prd.vo.UserBaseVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EntNoticeServiceImpl extends ServiceImpl<EntNoticeMapper, EntNotice> implements EntNoticeService {

    @Autowired
    private EntNoticeMapper entNoticeMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private EntNoticeRecipientService entNoticeRecipientService;

    @Override
    public int insertInfo(EntNotice entNotice) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entNotice);
        entNotice.setNumber(String.valueOf(System.nanoTime()));
        return entNoticeMapper.insert(entNotice);
    }

    @Override
    public int updateInfo(EntNotice entNotice) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entNotice);
        return entNoticeMapper.updateById(entNotice);
    }

    @Override
    public List<EntNotice> listInfo(String day, Integer type, String title) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        LambdaQueryWrapper<EntNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.apply(StringUtils.isNotBlank(day), "DATE_FORMAT(start_time,'%m-%d') = {0}", day);
        queryWrapper.eq(type != null, EntNotice::getType, type);
        queryWrapper.likeRight(StringUtils.isNotBlank(title), EntNotice::getTitle, title);
        queryWrapper.eq(EntNotice::getSigner, loginUser.getId());
        queryWrapper.or().apply("find_in_set({0},recipient)", loginUser.getId());
        queryWrapper.or().apply("find_in_set({0},make)", loginUser.getId());
        queryWrapper.or().eq(EntNotice::getCreateId, loginUser.getId());
        List<EntNotice> list = entNoticeMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public EntNoticeVo details(Long id) {
        EntNoticeVo entNoticeVo = new EntNoticeVo();
        EntNotice entNotice = entNoticeMapper.selectById(id);
        if (entNotice == null) {
            throw new RuntimeException("记录没有找到！");
        }
        BeanUtils.copyProperties(entNotice, entNoticeVo);

        //获取用户信息
        List<Long> arrayList = new ArrayList<>();
        List<Long> recipients = Arrays.asList(StringsUtils.convert(entNotice.getRecipient().split(",")));
        List<Long> makes = Arrays.asList(StringsUtils.convert(entNotice.getMake().split(",")));
        arrayList.addAll(makes);
        arrayList.stream().distinct().collect(Collectors.toList());
        List<UserBaseVo> listUser = userService.listName(arrayList.toArray(new Long[arrayList.size()]));
        List<String> listRecipient = new ArrayList<>();
        List<String> listMake = new ArrayList<>();
        for (UserBaseVo userBaseVo : listUser) {
            if (recipients.contains(userBaseVo.getId())) {
                listRecipient.add(userBaseVo.getName());
            }
            if (makes.contains(userBaseVo.getId())) {
                listMake.add(userBaseVo.getMerge());
            }
        }

        entNoticeVo.setRecipients(listRecipient);
        entNoticeVo.setMakes(listMake);
        return entNoticeVo;
    }

    @Override
    public int addMake(Long id, Long[] makes) {
        EntNotice entNotice = entNoticeMapper.selectById(id);
        if (entNotice == null) {
            throw new RuntimeException("记录没有找到！");
        }
        if (StringUtils.isNotBlank(entNotice.getMake())) {
            entNotice.setMake(entNotice.getMake() + "," + makes.toString());
        } else {
            entNotice.setMake(makes.toString());
        }
        return updateInfo(entNotice);
    }

    @Override
    public List<RecipientVo> recipient(Long id, Integer type) {
        EntNotice entNotice = entNoticeMapper.selectById(id);
        if (entNotice == null) {
            throw new RuntimeException("记录没有找到！");
        }
        LambdaQueryWrapper<EntNoticeRecipient> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntNoticeRecipient::getNoticeId, id);
        List<EntNoticeRecipient> recipientList = entNoticeRecipientService.list(queryWrapper);
        List<RecipientVo> list = new ArrayList<>();
        //获取用户信息
        List<Long> recipients = Arrays.asList(StringsUtils.convert(entNotice.getRecipient().split(",")));
        List<UserBaseVo> listUser = userService.listName(recipients.toArray(new Long[recipients.size()]));

        for (UserBaseVo userBaseVo : listUser) {
            RecipientVo recipientVo = new RecipientVo();
            BeanUtils.copyProperties(userBaseVo, recipientVo);
            for (EntNoticeRecipient recipient : recipientList) {
                if (recipient.getCreateId() == userBaseVo.getId()) {
                    recipientVo.setIsRead(IsStatus.ONE.getCode());
                } else {
                    recipientVo.setIsRead(IsStatus.TWO.getCode());
                }
            }
            list.add(recipientVo);
        }

//删除多余数据
        if (type == 1) {
            list.stream().filter(item -> item.getIsRead() == IsStatus.TWO.getCode()).collect(Collectors.toList());
        }
        if (type == 2) {
            list.stream().filter(item -> item.getIsRead() == IsStatus.ONE.getCode()).collect(Collectors.toList());
        }
        return list;
    }
}
